package com.剑指offer.no59;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Your MaxQueue object will be instantiated and called as such:
 * MaxQueue obj = new MaxQueue();
 * int param_1 = obj.max_value();
 * obj.push_back(value);
 * int param_3 = obj.pop_front();
 */
class MaxQueue {
    private Queue<Integer> queue;
    private Deque<Integer> sortQueue;

    public MaxQueue() {
        this.queue = new LinkedList<>();

        // 单调队列
        this.sortQueue = new LinkedList<>();
    }

    public int max_value() {
        if (sortQueue.isEmpty()) {
            return -1;
        }
        return sortQueue.peekFirst();
    }

    public void push_back(int value) {
        while (!sortQueue.isEmpty() && value > sortQueue.peekLast()) {
            sortQueue.pollLast();
        }
        sortQueue.offerLast(value);
        queue.offer(value);
    }

    public int pop_front() {
        if (queue.isEmpty()) {
            return -1;
        }
        int res = queue.poll();
        if (!sortQueue.isEmpty() && res == sortQueue.peekFirst()) {
            sortQueue.pollFirst();
        }

        return res;
    }

    public static void main(String[] args) {
        MaxQueue obj = new MaxQueue();
        int param_1 = obj.max_value();
        obj.push_back(1);
        int param_3 = obj.pop_front();
    }
}


public class Solution {

}
